package com.woniu.controller;


import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.UserLeagueClassMarginDto;
import com.woniu.model.form.AddLeagueClassMarginForm;
import com.woniu.model.model.YUserLeagueClassMargin;
import com.woniu.service.YUserLeagueClassMarginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yUserLeagueClassMargin")
@Api(tags = "用户团课余量接口")
public class YUserLeagueClassMarginController {

    @Resource
    private YUserLeagueClassMarginService userLeagueClassMarginService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增用户的团课余量
     * @param form
     * @return
     */
    @PostMapping("/addLeagueClassMargin")
    @ApiOperation("新增用户的团课余量")
    public Result<?> addLeagueClassMargin(@RequestBody AddLeagueClassMarginForm form) {

        Integer i = userLeagueClassMarginService.addLeagueClassMargin(form);

        if (!i.equals(1)){
            return Result.getResult().setCode(500).setMsg("新增失败");
        }

        return Result.getResult().setMsg("新增成功");
    }

    /**
     * 根据订单查询余课对象
     * @param orderNum
     * @return
     */
    @GetMapping("/loadLeagueClassMarginByOrderNum")
    @ApiOperation("根据订单查询余课对象")
    public Result<YUserLeagueClassMargin> loadLeagueClassMarginByOrderNum(String orderNum){
        YUserLeagueClassMargin userLeagueClassMargin = userLeagueClassMarginService.loadLeagueClassMarginByOrderNum(orderNum);

        return Result.newInstance(YUserLeagueClassMargin.class).setData(userLeagueClassMargin);
    }

    /**
     * 根据教练id查询学员
     * @param coachId
     * @return
     */
    @GetMapping("/loadLeagueStudentIds")
    @ApiOperation("根据教练id查询学员")
    public Result<List<Integer>> loadLeagueStudentIds(Integer coachId){
        List<Integer> studentIds = userLeagueClassMarginService.loadLeagueStudentIds(coachId);

        return Result.newListInstance(Integer.class).setData(studentIds);
    }

    /**
     * 用户根据id查询用户团课
     * @param token
     * @return
     */
    @GetMapping("/loadLeagueClassByUserToken")
    @ApiOperation("用户根据id查询用户团课")
    public Result<List<UserLeagueClassMarginDto>> loadLeagueClassByUserToken(@RequestHeader String token, String leagueClassName){
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        List<UserLeagueClassMarginDto> margins = userLeagueClassMarginService.loadLeagueClassByUserToken(userId,leagueClassName);

        return Result.newListInstance(UserLeagueClassMarginDto.class).setData(margins);
    }

}

